package com.example.excel_db.validator;

import com.example.excel_db.entity.ExcelTemplate;
import com.example.excel_db.entity.ExcelTemplateField;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 抽象Excel数据校验器
 * 提供基础的校验功能
 */
@Slf4j
public abstract class AbstractExcelDataValidator implements ExcelDataValidator {

    protected ExcelTemplate template;
    protected List<ExcelTemplateField> fields;
    
    /**
     * 构造函数
     * 
     * @param template Excel模板
     * @param fields 模板字段列表
     */
    public AbstractExcelDataValidator(ExcelTemplate template, List<ExcelTemplateField> fields) {
        this.template = template;
        this.fields = fields;
    }

    @Override
    public List<String> validateRow(Map<String, Object> rowData, int rowIndex) {
        List<String> errors = new ArrayList<>();
        
        // 校验必填字段
        for (ExcelTemplateField field : fields) {
            if (field.isRequired()) {
                Object value = rowData.get(field.getFieldName());
                if (value == null || (value instanceof String && ((String) value).trim().isEmpty())) {
                    errors.add(String.format("第%d行: 字段 '%s' 不能为空", rowIndex + 1, field.getFieldColumn()));
                }
            }
        }
        
        // 执行子类的自定义校验
        List<String> customErrors = doValidateRow(rowData, rowIndex);
        if (customErrors != null && !customErrors.isEmpty()) {
            errors.addAll(customErrors);
        }
        
        return errors;
    }

    @Override
    public Map<Integer, List<String>> validateBatch(List<Map<String, Object>> dataList) {
        Map<Integer, List<String>> errorMap = new ConcurrentHashMap<>();
        
        // 并行处理校验
        dataList.parallelStream().forEach(rowData -> {
            int rowIndex = dataList.indexOf(rowData);
            List<String> errors = validateRow(rowData, rowIndex);
            if (!errors.isEmpty()) {
                errorMap.put(rowIndex, errors);
            }
        });
        
        // 执行子类的自定义批量校验
        Map<Integer, List<String>> customErrors = doValidateBatch(dataList);
        if (customErrors != null && !customErrors.isEmpty()) {
            customErrors.forEach((rowIndex, errList) -> {
                errorMap.computeIfAbsent(rowIndex, k -> new ArrayList<>()).addAll(errList);
            });
        }
        
        return errorMap;
    }
    
    /**
     * 子类实现的自定义行校验
     * 
     * @param rowData 行数据
     * @param rowIndex 行索引
     * @return 校验错误信息列表
     */
    protected abstract List<String> doValidateRow(Map<String, Object> rowData, int rowIndex);
    
    /**
     * 子类实现的自定义批量校验
     * 默认实现为空，子类可以覆盖此方法实现批量校验逻辑
     * 
     * @param dataList 数据列表
     * @return 校验错误信息映射
     */
    protected Map<Integer, List<String>> doValidateBatch(List<Map<String, Object>> dataList) {
        return new HashMap<>();
    }
}